package zpx.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.compass.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import zpx.iservice.IComAttachmentService;
import zpx.model.ComAttachment;
import zpx.util.Constants;



/**
 * ��������controller
 * 
 */
@Controller
@RequestMapping("/fileOper")
public class FileController {

	private IComAttachmentService comAttachmentService;

	public IComAttachmentService getComAttachmentService() {
		return comAttachmentService;
	}
	
	@Resource
	public void setComAttachmentService(IComAttachmentService comAttachmentService) {
		this.comAttachmentService = comAttachmentService;
	}
    /**
     * ����ļ���Ϣ��Ϣ
     * 
     * @param request
     * @param response
     * @return Map
     */
    @RequestMapping(value = "/getFile", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public void getFile(String attaId, HttpServletRequest request, HttpServletResponse response) {
        if (!StringUtils.hasText(attaId)) {
            return;
        }
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {

            ComAttachment comAttachment = this.comAttachmentService.getAttachment(attaId);
            if (comAttachment != null) {
                String filePath = Constants.ATTR_PATH + Constants.FILE_SEP
                        + comAttachment.getAttaPath();
                File file = new File(filePath);
                response.setContentType("text/html;charset=utf-8");
                request.setCharacterEncoding("UTF-8");
                response.setContentType("application/x-msdownload;");
                response.setHeader(
                        "Content-disposition",
                        "attachment; filename="
                                + encodeFileName(request, comAttachment.getAttaName()));
                response.setHeader("Content-Length", String.valueOf(file.length()));

                inputStream = new FileInputStream(file);
                outputStream = response.getOutputStream();
                byte[] bStream = new byte[1000];
                int j = 0;
                while ((j = inputStream.read(bStream, 0, 1000)) != -1) {
                    outputStream.write(bStream);
                }

                outputStream.flush();
            }

        } catch (Exception e) {
            e.printStackTrace();
            //do nothing
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                //do nothing
            }
        }
    };

    /**
     * ���ļ�����룬���������������
     * @param request
     * @param fileName
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encodeFileName(HttpServletRequest request, String fileName)
            throws UnsupportedEncodingException {
        String agent = request.getHeader("USER-AGENT");
        if (null != agent && -1 != agent.indexOf("MSIE")) {
            return URLEncoder.encode(fileName, "UTF8");
        } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
            return "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8"))))
                    + "?=";
        } else {
            return fileName;
        }
    }
}

